//
// Created by weslywang on 2020/4/30.
//
#include "../common/common.h"

Node *copyNode(Node *tree) {
    if (!tree) {
        return NULL;
    }
    Node *node = new Node(tree->val);
    node->left = copyNode(tree->left);
    node->right = copyNode(tree->right);
    if (tree->next) {
        node->next = new Node(tree->next->val);
    } else {
        node->next = NULL;
    }
    return node;
}

Node *connect(Node *root) {
    if (!root) {
        return root;
    }
    queue<Node *> list;
    Node *front, *pos = root;
    list.push(root);
    while (!list.empty()) {
        front = list.front();
        list.pop();
        if (front->left) {
            list.push(front->left);
        }
        if (front->right) {
            list.push(front->right);
        }
        if (front == pos) {
            pos = front->right;
            front->next = NULL;
        } else if (list.front()) {
            front->next = list.front();
        } else {
            front->next = NULL;
        }
    }
    return root;
}

int main() {
    Common common;
    int a[] = {1, 2, 3, 4, 5};
    vector<vector<int>> numList = common.loadData();
    for (int i = 0; i < numList.size(); i++) {
        TreeNode *root = common.makeTree(numList[i]);
        Node *node = common.treeNodeToNode(root);
        connect(node);
        cout << "**************" << endl;
    }
    return 0;
}